package com.lenovo.leos.filebrowser.netdisk;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;

import com.lenovo.leos.filebrowser.R;
import com.lenovo.leos.letools.db.AllUploadTaskEntity;
import com.lenovo.leos.letools.db.FileDownloadEntity;
import com.lenovo.leos.letools.db.FileDownloadService;
import com.lenovo.leos.letools.db.FileUploadEntity;
import com.lenovo.leos.letools.lenovodata.DownLoadServer;
import com.lenovo.leos.letools.lenovodata.UploadServer;

public class SumTask {

	private static final String TAG = "SumTask";
	private static final boolean TAG_FLAT = true;
	private DownLoadServer downLoadServer;
	private UploadServer uploadServer;
	List<FileDownloadEntity> fileTasks;
	Map<String, List<FileDownloadEntity>> dirTasks;
	AllUploadTaskEntity allUploadTask;
	public static final int NUM = 20;
	private long netFreeSpace;//网盘剩余空间大小
	public static final int UPLOAD_RUN_COUNT = 0;
	public static final int UPLOAD_PAUSE_COUNT = 1;
	public static final int UPLOAD_COMPLETE_COUNT = 2;
	
	public static final int DOWNLOAD_RUN_COUNT = 0;
	public static final int DOWNLOAD_PAUSE_COUNT = 1;
	public static final int DOWNLOAD_COMPLETE_COUNT = 2;

	private static SumTask instance = new SumTask();

	private int sumTask;
	
	public SumTask(){
		
	}
	
	public void setContext(Context context){
		downLoadServer = DownLoadServer.getInstance(context);
		uploadServer = UploadServer.getInstance(context);
		// 从数据库中得到所有的下载任务
		fileTasks = downLoadServer.getFileLoadTask();
		// 从数据库中得到所有的文件夹下载任务
		dirTasks = downLoadServer.getDirLoadTask();
		// 从数据库中得到所有的文件夹和文件上传任务
		allUploadTask = uploadServer.getAllTask();
//		for(int i=0;i<2;i++){
//			Log.d("SumTask","################################# fileTasks size is: " + fileTasks.size() + ",dirTasks size is: " + dirTasks.size()
//					+ ",allUploadTask's fileTask size is: " + allUploadTask.getFileTasks().size() + ",allUploadTask's dirTask size is:" 
//					+ allUploadTask.getDirTasks().size());
//		}
	}

	public static SumTask getInstance() {
		return instance;
	}

	public int getSumTask() {
		sumTask = fileTasks.size() + dirTasks.size()
				+ allUploadTask.getFileTasks().size()
				+ allUploadTask.getDirTasks().size();
		if(TAG_FLAT){
			Log.d(TAG, "the sumTask is ============= " + sumTask);
		}
		return sumTask;
	}

	public long getNetFreeSpace() {
		return netFreeSpace;
	}

	public void setNetFreeSpace(long netFreeSpace) {
		this.netFreeSpace = netFreeSpace;
	}
	
	//得到所有下载任务的状态总数，返回格式是total_run;total_pause;total_complete
	public String getDownloadTaskTotalCount(Context context){
		setContext(context);
		
		StringBuilder num_str = new StringBuilder();
		int total_run = 0;
		int total_pause = 0;
		int total_complete = 0;
		
		//first,loop all file task
		for(int i=0;i<fileTasks.size();i++){
			FileDownloadEntity entity = fileTasks.get(i);
			//如果正在下载并且没有下载完成，则是正在下载
			if(1 == entity.getIsloading() && 0 == entity.getLoadover()){
				total_run++;
			}
			//如果没有下载，并且没有完成，则是暂停
			else if(0 == entity.getIsloading() && 0 == entity.getLoadover()){
				total_pause++;
			}else if(1 == entity.getLoadover()){
				total_complete++;
			}
		}
		//second,loop all dir task
		Set<Entry<String, List<FileDownloadEntity>>> entrys = dirTasks.entrySet();
		for (Entry<String, List<FileDownloadEntity>> entry : entrys) {
			if(isDownLoadDirRun(entry)){
				total_run++;
			}else if(isDownLoadDirPause(entry)){
				total_pause++;
			}else if(isDownLoadDirComplete(entry)){
				total_complete++;
			}
		}
		
		num_str.append(total_run).append(";").append(total_pause).append(";").append(total_complete).append(";");
		return num_str.toString();
		
	}
	
	//得到所有上传任务的状态总数，返回格式是total_run;total_pause;total_complete
	public String getUploadTaskTotalCount(Context context){
		setContext(context);
		StringBuilder num_str = new StringBuilder();
		int total_run = 0;
		int total_pause = 0;
		int total_complete = 0;
		
		//遍历所有上传文件任务
		List<FileUploadEntity> entities = allUploadTask.getFileTasks();
		
		for(int i=0;i<entities.size();i++){
			FileUploadEntity entity = entities.get(i);
			//如果正在下载并且没有下载完成，则是正在下载
			if(1 == entity.getIsloading() && (2 != entity.getFileState())){
				total_run++;
			}
			//如果没有下载，并且没有完成，则是暂停
			else if(0 == entity.getIsloading() && (2 != entity.getFileState()) ){
				total_pause++;
			}else if(2 == entity.getFileState()){
				total_complete++;
			}
		}
		
		//遍历所有上传文件夹任务
		Set<Entry<String, List<FileUploadEntity>>> entrys =  allUploadTask.getDirTasks().entrySet();
		for (Entry<String, List<FileUploadEntity>> entry : entrys) {
			if(isUpLoadDirRun(entry)){
				total_run++;
			}else if(isUpLoadDirPause(entry)){
				total_pause++;
			}else if(isUpLoadDirComplete(entry)){
				total_complete++;
			}
		}
		num_str.append(total_run).append(";").append(total_pause).append(";").append(total_complete).append(";");
		return num_str.toString();
	}
	
	/**
	 * 判断下载的任务个数：
		对于文件夹任务，遍历循环这个文件夹下的所有子文件：
		a:如果有一个子文件的isLoading为true,则说明该文件夹任务正在下载；
		b:如果所有子文件的loadover为true,则说明该文件已经下载完成
		c:如果有一个子文件的loadover不为true,并且isLoading不为true,则则说明该文件处于暂停状态。

	 * @param entry
	 * @return
	 */
	private boolean isDownLoadDirRun(Entry<String, List<FileDownloadEntity>> entry){
		boolean isLoading = false;
		for (FileDownloadEntity fde : entry.getValue()) {
			if(1 == fde.getIsloading()){
				isLoading = true;
				break;
			}
		}
		return isLoading;
	}
	
	private boolean isDownLoadDirPause(Entry<String, List<FileDownloadEntity>> entry){
		boolean isPause = false;
		for (FileDownloadEntity fde : entry.getValue()) {
			if(0 == fde.getIsloading() && 0 == fde.getLoadover()){
				isPause = true;
				break;
			}
		}
		return isPause;
	}
	
	private boolean isDownLoadDirComplete(Entry<String, List<FileDownloadEntity>> entry){
		boolean isComplete = true;
		for (FileDownloadEntity fde : entry.getValue()) {
			if(0  == fde.getLoadover()){
				isComplete = false;
				break;
			}
		}
		return isComplete;
	}
	
	
	
	/**
	 * 判断上传的任务个数：
		对于文件夹任务，遍历循环这个文件夹下的所有子文件：
		a:如果有一个子文件的isLoading为true,则说明该文件夹任务正在上传；
		b:如果所有子文件的loadover为true,则说明该文件已经上传完成
		c:如果有一个子文件的loadover不为true,并且isLoading不为true,则则说明该文件处于暂停状态。

	 * @param entry
	 * @return
	 */
	private boolean isUpLoadDirRun(Entry<String, List<FileUploadEntity>> entry){
		boolean isLoading = false;
		for (FileUploadEntity fde : entry.getValue()) {
			if(1 == fde.getIsloading()){
				isLoading = true;
				break;
			}
		}
		return isLoading;
	}
	
	private boolean isUpLoadDirPause(Entry<String, List<FileUploadEntity>> entry){
		boolean isPause = false;
		for (FileUploadEntity fde : entry.getValue()) {
			if(0 == fde.getIsloading() && (2 != fde.getFileState())){
				isPause = true;
				break;
			}
		}
		return isPause;
	}
	
	private boolean isUpLoadDirComplete(Entry<String, List<FileUploadEntity>> entry){
		boolean isComplete = true;
		for (FileUploadEntity fde : entry.getValue()) {
			if(2  != fde.getFileState()){
				isComplete = false;
				break;
			}
		}
		return isComplete;
	}
}
